﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Day
{
    /// <summary>
    /// 同批次交易员均值
    /// </summary>
    public class ServiceBatchAvgDay : Benefit.Interface.Day.IBatchAvgDay
    {
        Benefit.DB.DBManager db = null;
        public ServiceBatchAvgDay(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除同批次交易员均值数据
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.BatchAvgDay where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.BatchAvgDay d in query)
            {
                db.BatchAvgDay.Remove(d);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 保存同批次交易员均值数据
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitBatchDayAvg(int tradeHistoryId, string pdate)
        {
            var query = (from t in db.Batches select t).ToList();
            Interface.Sys.IDayOperatorAccount idoa = new Sys.ServiceDayOperatorAccount(db);
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            foreach (Models.Sys.Batch batch in query)
            {
                Models.Day.BatchAvgDay batchavgday = new Models.Day.BatchAvgDay();
                batchavgday.TradeHistoryId = tradeHistoryId;
                batchavgday.BatchId = batch.Id;

                List<Models.Day.AccountDayChange> change1 = new List<Models.Day.AccountDayChange>();
                List<Models.Day.AccountDayAnalysis> change2 = new List<Models.Day.AccountDayAnalysis>();

                var _query = (from t in db.Operators where t.BatchId == batch.Id && t.IsDeleted == false select t).ToList();
                foreach (Models.Sys.Operator oper in _query)
                {
          
       
                    List<Account> accounts = idoa.GetDayOperatorAccounts(ith.GetPdateFromTradeHistoryId(tradeHistoryId), oper.Id);
                    foreach (Account acc in accounts)
                    {
                        var query1 = from t in db.AccountDayChange where t.AccountId == acc.Id && t.TradeHistoryId == tradeHistoryId select t;
                        if (query1.Count() > 0)
                        {
                            change1.Add(query1.First());
                        }

                        var query2 = from t in db.AccountDayAnalysis where t.AccountId == acc.Id && t.TradeHistoryId == tradeHistoryId select t;
                        if (query2.Count() > 0)
                        {
                            change2.Add(query2.First());
                        }
                    }
                }
                if (change1.Count > 0)
                {
                    batchavgday.BillCount = Math.Round(change1.Sum(a => a.BillCount) / _query.Count(), 2);
                    batchavgday.DayCount = Math.Round(change1.Sum(a => a.DayCount) / _query.Count(), 2);
                    batchavgday.Free = Math.Round(change1.Sum(a => a.Free) / _query.Count(), 2);
                    batchavgday.LoseCount = Convert.ToInt32(change1.Sum(a => a.LoseCount) / _query.Count());
                    batchavgday.Profit = Math.Round(change1.Sum(a => a.Profit) / _query.Count(), 2);
                    batchavgday.SumLost = Math.Round(change1.Sum(a => a.SumLost) / _query.Count(), 2);
                    batchavgday.SumWin = Math.Round(change1.Sum(a => a.SumWin) / _query.Count(), 2);
                    batchavgday.UsedMargin = Math.Round(change1.Sum(a => a.UsedMargin) / _query.Count(), 2);
                    batchavgday.WinCount = Convert.ToInt32(change1.Sum(a => a.WinCount) / _query.Count());
                }

                if (change2.Count > 0)
                {
                    batchavgday.AvgBillBenefit = Math.Round(change2.Sum(a => a.AvgBillBenefit) / _query.Count(), 2);
                    batchavgday.AvgPostionTime = Math.Round(change2.Sum(a => a.AvgPostionTime) / _query.Count(), 2);
                    batchavgday.MaxReturn = Math.Round(change2.Sum(a => a.MaxReturn) / _query.Count(), 2);
                    batchavgday.OverStopLoss = Math.Round(change2.Sum(a => a.OverStopLoss) / _query.Count(), 2);
                    batchavgday.PingJinPing = Convert.ToInt32(change2.Sum(a => a.PingJinPing) / _query.Count());
                    batchavgday.RaiseRate = Math.Round(change2.Sum(a => a.RaiseRate) / _query.Count(), 2);
                    batchavgday.RiskCount = Convert.ToInt32(change2.Sum(a => a.RiskCount) / _query.Count());
                    batchavgday.Utilization = Math.Round(change2.Sum(a => a.Utilization) / _query.Count(), 2);
                    batchavgday.WinRate = Math.Round(change2.Sum(a => a.WinRate) / _query.Count(), 2);
                    batchavgday.Yield = Math.Round(change2.Sum(a => a.Yield) / _query.Count(), 2);
                }
                db.BatchAvgDay.Add(batchavgday);
            }
            db.SaveChanges();
        }


        public Models.Day.BatchAvgDay GetBatchAvgDay(int operatorId, int tradeHistoryId)
        {
            var batchquery = db.Operators.Find(operatorId);
            if (batchquery != null)
            {
                var query = from t in db.BatchAvgDay where t.TradeHistoryId == tradeHistoryId && t.BatchId == batchquery.BatchId select t;

                if (query.Count() > 0)
                {
                    var batchday = query.First();
                    return batchday;

                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
    }
}